using DG.Tweening;
using HCore.IOC.Context.Event.Attr;
using HCore.Unity.Behaviour;
using HCore.Unity.Core;
using JetBrains.Annotations;
using UnityEngine;

namespace UI.UIGame
{
    /// <summary>
    /// 如果逻辑多了。可以拆分成单独的类来处理
    /// </summary>
    public class UIGameVisual : HCoreMonoBehaviour
    {
        #region EventListener

        #region Router Event

        [UsedImplicitly]
        [EventListener]
        private void OnRouterEvent(RouterEvent routerEvent)
        {
            // 初始化
            if (routerEvent.NewRouter == Router.None && routerEvent.OldRouter == Router.None)
                // 初始化时：隐藏游戏场景的UI界面
                GameUtils.UIGameCanvas.gameObject.SetActive(false);

            // 进入 游戏界面
            if (routerEvent.NewRouter == Router.GamePage)
            {
                // 允许移动相机
                GameManager.EnableCameraMove();
                // 显示游戏界面的 UI
                GameUtils.UIGameCanvas.gameObject.SetActive(true);
            }

            // 离开 游戏界面
            if (routerEvent.OldRouter == Router.GamePage)
                // 隐藏游戏界面的 UI
                GameUtils.UIGameCanvas.gameObject.SetActive(false);
        }

        #endregion

        #region BackToPuzzleStartPoint Event

        private Tweener mBackToPuzzleStartPointTweener;

        [UsedImplicitly]
        [EventListener(EventExecuteMode.Coroutine)]
        private void OnBackToPuzzleStartPointEvent(BackToPuzzleStartPoint eventData)
        {
            if (mBackToPuzzleStartPointTweener != null) return;
            var mainCameraTransform = GameUtils.MainCamera.transform;
            var targetPos = new Vector3(0, 0, mainCameraTransform.position.z); // 中心点
            mBackToPuzzleStartPointTweener = mainCameraTransform.DOMove(targetPos, 0.5f)
                .SetEase(Ease.OutCubic)
                .OnStart(GameManager.DisabledCameraMove)
                .OnKill(() =>
                {
                    GameManager.EnableCameraMove();
                    mBackToPuzzleStartPointTweener = null;
                });
        }

        #endregion

        #endregion
    }
}